/*
 * Python初始化, 使用Python脚本进行Python的支持路径初始化
 *
 * 历史:
 *  2020-02-27 姚彧 更新Python初始化脚本
 */

#include "python_config.h"
#include "python_init.h"

void app_initialize_python()
{
	Py_Initialize();
    char script[] = R"(
def update_sys_path(py):
    import os
    import sys

    def update_path(base):
        i=3
        while (i>0):
            py_dir = os.path.join(base, py)
            if os.path.isdir(py_dir):
                if py_dir not in sys.path:
                    print("sys.path.append:", py_dir)
                    sys.path.append(py_dir)
                return 1
            i-=1
            base = os.path.dirname(base)

    for base in reversed(sys.path):
        base = os.path.abspath(base)
        if os.path.join('python', 'python') not in base.lower():
            break
    for base in (base, os.getcwd()):
        if(base and update_path(base)):
            return
    print("%s python plugin not exists." % py)
    sys.exit(0)

update_sys_path("py")
del update_sys_path
import app
)";
    PyRun_SimpleString(script);
}

int app_finalize_python()
{
	return Py_FinalizeEx();
}
